package com.example.vae.Home.Home;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.vae.Adapter.MyFragmentAdapter;
import com.example.vae.HomeViewModel;
import com.example.vae.R;
import com.example.vae.databinding.HomeFragmentBinding;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment implements ViewPager.OnPageChangeListener, View.OnClickListener {

    //数据绑定
    private HomeViewModel mViewModel;
    private HomeFragmentBinding binding;

    //    viewpager绑定
    private ViewPager viewPager;
    private MyFragmentAdapter myHomePageAdapter;
    private List<Fragment> fragmentList;


    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewPager.removeOnPageChangeListener(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
//       绑定首页面的数据
        mViewModel = ViewModelProviders.of(requireActivity()).get(HomeViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.home_fragment, container, false);
        binding.setData(mViewModel);
//        设置数据变化 的观察
        binding.setLifecycleOwner(requireActivity());


//加载数据中的 pageview 变化
        viewPager = binding.homePage;
//        添加三个页面
        fragmentList = new ArrayList<>();
        fragmentList.add(new dongtaiFragment());
        fragmentList.add(new HuodongFragment());
        fragmentList.add(new XingchengFragment());
//      添加适配器
        myHomePageAdapter = new MyFragmentAdapter(this.getFragmentManager(), 0, fragmentList);
//设置缓存 3个
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(myHomePageAdapter);
        viewPager.setCurrentItem(0);
//        添加事件监听
        viewPager.addOnPageChangeListener(this);
        binding.xingcheng.setOnClickListener(this);
        binding.huodong.setOnClickListener(this);
        binding.dongtai.setOnClickListener(this);


//        list=new ArrayList<>();
//        LayoutInflater layoutInflater=getLayoutInflater();
//        list.add(layoutInflater.inflate(R.layout.dongtai,null,false));
//        list.add(layoutInflater.inflate(R.layout.xingchen,null,false));
//        list.add(layoutInflater.inflate(R.layout.huodong,null,false));
//        listtitle=new ArrayList<>();
//        listtitle.add("动态");
//        listtitle.add("行程");
//        listtitle.add("活动");
//
//
//        myHomePageAdapter=new MyFragmentAdapter(list,listtitle);
//        viewPager.setAdapter(myHomePageAdapter);


        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // TODO: Use the ViewModel
    }

    //    点击的时候发生的事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dongtai:
                viewPager.setCurrentItem(0, true);
                break;
            case R.id.xingcheng:
                viewPager.setCurrentItem(1, true);
                break;
            case R.id.huodong:
                viewPager.setCurrentItem(2, true);
                break;
            default:
                break;


        }

    }

    public void TextColor(int position){
        switch (position) {
            case 0:

                binding.dongtai.setTextColor(getResources().getColor(R.color.smallBlue));
                binding.huodong.setTextColor(getResources().getColor(R.color.text_black));
                binding.xingcheng.setTextColor(getResources().getColor(R.color.text_black));
                break;
            case 1:
                binding.dongtai.setTextColor(getResources().getColor(R.color.text_black));
                binding.huodong.setTextColor(getResources().getColor(R.color.text_black));
                binding.xingcheng.setTextColor(getResources().getColor(R.color.smallBlue));
                break;
            case 2:
                binding.dongtai.setTextColor(getResources().getColor(R.color.text_black));
                binding.huodong.setTextColor(getResources().getColor(R.color.smallBlue));
                binding.xingcheng.setTextColor(getResources().getColor(R.color.text_black));
                break;
        }
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //    当页面被选着时，进行的操作
    @Override
    public void onPageSelected(int position) {
//        修改字体颜色
        TextColor(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
